package com.i2bt.util;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.bean.FriendBean;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class FriendMessageUtil {
	
	private final static String CODE = "%";		//分隔符
	
	public final static int REQUEST = 1;		//请求
	
	public final static int ACCEPT = 2;		//同意
	
	public final static int REFUSE = 3;		//拒绝
	
	private final static String SENDER = CODE + 
			AndroidRestContract.FRIEND_MESSAGE_SENDER + CODE;		//发送者的代码
	
	private final static String RECEIVER = CODE + 
			AndroidRestContract.FRIEND_MESSAGE_RECEIVER + CODE;		//接受者的代码
	
	private final static String YOU = "您";
	
	private final static String OTHER = "对方";
	
	/**
	 * 解码接受和响应好友消息的字符串里面的代号
	 * @param friendBean 信息类
	 * @return 处理好的content
	 */
	public static void messageContentDecoded(FriendBean friendBean) {
		String content = friendBean.getContent();
		String sender = friendBean.getSender();
		String receiver = friendBean.getReceiver();
		if (sender.equals(UserConfig.userName)) {
			//你发送的
			content = content.replace(RECEIVER, OTHER);
			content = content.replace(SENDER, YOU);
		}
		else if (receiver.equals(UserConfig.userName)) {
			//对方发送的
			content = content.replace(SENDER, OTHER);
			content = content.replace(RECEIVER, YOU);
		}
		friendBean.setContent(content);
		
	}
	
	/**
	 * 编码接受和响应好友消息的字符串里的发送者和接受者
	 * @param friendBean 信息类
	 * @return 处理好的content
	 */
	public static void messageContentEecoded(FriendBean friendBean) {
		String content = "";
		String sender = friendBean.getSender();
		String receiver = friendBean.getReceiver();
		int flag = friendBean.getFlag();
		
		// 编码内容
		if (sender.equals(UserConfig.userName)) {
			
			switch (flag) {
			
			case REQUEST : {
				content = SENDER + "向" + RECEIVER + "发送关注请求";
				break;
			}
			
			case ACCEPT : {
				content = RECEIVER + "接受" + SENDER + "的关注请求"; 
				break;
			}
			
			case REFUSE : {
				content = RECEIVER + "拒绝" + SENDER + "的关注请求";
				break;
			}
			
			}
		}
		else if (receiver.equals(UserConfig.userName)) {
			
			switch (flag) {
			
			case REQUEST:
				content = RECEIVER + "向" + SENDER + "发送关注请求";
				break;

			case ACCEPT : {
				content =  RECEIVER + "接受" + SENDER + "的关注请求";
				break;
			}
			
			case REFUSE : {
				content =  RECEIVER + "拒绝" + SENDER + "的关注请求";
				break;
			}
				
			}
			
		}

		// 对接受者, 发送者, 内容做编码
		try {
			friendBean.setContent(URLEncoder.encode(content, "utf-8"));
			friendBean.setSender(URLEncoder.encode(sender, "utf-8"));
			friendBean.setReceiver(URLEncoder.encode(receiver, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}
	
}
